<!doctype html>
<html>
<head>
	<meta charset="utf-8"><meta name="format-detection" content="telephone=no">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
	<title>课堂</title>
	<link rel="stylesheet" type="text/css" href="../../lib/css/skin.css"/>
</head>
<body>
<div id="root">
	<div class="app_warp">
		<div class="app_head">
			<span class="left back"></span>
			<span class="input">
				<input id="input" type="text" placeholder="输入关键字搜索">
			</span>
			<span id="close_search" class="right close_search"></span>
		</div>
		<div class="app_col_nav clearfix" style="display: none;">
			<div id="classroom_nav" class="app_col_inner_nav loaded menuflex"></div>
		</div>
		<div id="search_main" class="app_main clearfix" style="padding: 0 0 .5rem"></div>
		<div class="loading_line animated fadeInDown hide" style="padding: .5rem 0;"><a href="javascript:;" tapmode onclick="loadmore()" class="loading_more"></a></div>
	</div>
</div>

<script id="home_top_news_template" type="text/html">
	<div class="bgw" style="padding: 0 1rem">
		<ul class="list_item line_list clearfix">
		<%for(var i in data) {%>
			<li datda_id="<%:=data[i].id%>" class="listnumitem">
				<img class="pic imgcache" src="<%:=img%>" data_src="<%:=data[i].cover%>" alt="" tapmode onclick="_$.getPage('<%:=data[i].href%>')">
				<span class="title" tapmode onclick="_$.getPage('<%:=data[i].href%>')">
					<%:=data[i].title%>
				</span>
				<span class="intro">
					<%:=data[i].author%> <%:=data[i].time%>
				</span>
			</li>
		<%}%>
		</ul>
	</div>
</script>
<script id="classroom_list_template" type="text/html">
	<div class="bgw" style="padding: 0 1rem">
		<ul class="list_item line_list biger clearfix">
			<%for(var i in data) {%>
			<li tapmode onclick="_$.getView('class',<%:=data[i].id%>,this)" class="listnumitem">
				<img class="pic imgcache" src="<%:=img%>" data_src="<%:=data[i].cover%>" alt="">
				<span class="title">
					<%:=data[i].title%>
				</span>
				<span class="intro">
					<%:=data[i].author%> <%:=data[i].viewnum%>观看
				</span>
				<span class="tag tag_skin"><%:=data[i].integral===0?'免积分':(data[i].integral+'积分观看')%></span>
			</li>
			<%}%>
		</ul>
	</div>
</script>
<script id="artcircle_list_template" type="text/html">
	<ul class="list_item sociality_list clearfix">
		<%for(var i in data) {
		var _data_ = data[i];
		%>
		<li class="listnumitem <%:getClassid=data[i].classid%>" tapmode onclick="_$.viewArtcircle(this,<%:=data[i].id%>)">
			<img class="sociality_pic imgcache" src="<%:=img%>" data_src="<%:=data[i].avatar%>" alt="">
			<span class="sociality_username">
				<%:=_data_.author%>
			</span>
			<span class="sociality_date">
				<%:=_data_.date%>
			</span>
			<span class="sociality_zan <%:=data[i].zan.end===true?'active':'unactive'%>">
				<%:=_data_.zan.num%>
			</span>
			<span class="sociality_comment">
				<%if(_data_.comment!==null){%>
					<%:=_data_.comment.length%>
				<%}else{%>
					0
				<%}%>
			</span>
			<%if(_data_.intro!==null){%>
			<div class="sociality_cont clearfix">
				<%:=_data_.intro%>
			</div>
			<%}%>
			<%if(_data_.pics!==null){%>
			<div class="sociality_imgs clearfix">
				<%for(var j in _data_.pics) { var _pics_ = _data_.pics[j]%>
				<img class="imgcache" src="<%:=img%>" data_src="<%:=_pics_.thumb%>" picture="<%:=_pics_.picture%>" alt="">
				<%}%>
			</div>
			<%}%>
		</li>
		<%}%>
	</ul>
</script>
<script id="mechanism_list_template" type="text/html">
	<ul class="list_item line_list biger clearfix">
		<%for(var i in data) {%>
		<%
		var _data_ = data[i],
		_thisClasses_ = '';
		for(var _i_ in _data_.classid){
		_thisClasses_+='classid_'+ _data_.classid[_i_]+' '
		}
		_thisClasses_ += 'areaid_'+_data_.areaid;
		%>
		<li class="listnumitem <%=_thisClasses_%>" tapmode onclick="_$.getView('mechanism',<%:=_data_.id%>,this)" data_id="<%:=_data_.id%>">
			<img class="pic imgcache" src="<%:=img%>" data_src="<%:=_data_.cover%>" alt="">
			<span class="comname">
				<%:=_data_.name%>
			</span>
			<span class="intention">
				<%
					for(var j in _data_.classid) {
						var _classid_ = _data_.classid[j]
						if(j<2){ %>
							<%:getClassifyName=_classid_%>
						<%}else if(_data_.classid.length>2 && j==2){%>
							等...
						<% } %>
					<% } %>
				 / <%:getAreaName=_data_.areaid%>

			</span>
			<span class="intro">
				<%:subStr16=_data_.intro%>
			</span>
		</li>
		<%}%>
	</ul>
</script>

<script id="school_search_template" type="text/html">
	<ul id="yuanxiaolist" class="yuanxiaolist bgw" style="height: auto;padding: 0 .25rem .25rem;">
	<%for(var i in data){%>
	<li class="listnumitem" tapmode onclick="api.openWin({name:'yuanxiao_view',url:'../yuanxiao/yuanxiao_view.html',pageParam:{id:<%:=data[i].id%>}})">
		<img class="imgcache" src="<%:=img%>" data_src="<%:=data.src%><%:=data[i].logo%>" alt="">
		<%:subStr=data[i].name%>
	</li>
	<%}%>
	</ul>
</script>

<script id="classify_init_template" type="text/html">
	<%for(var i in data) {%>
		<a href="javascript:;" tapmode onclick="setSearchID(<%:=data[i].id%>,this)" class="<%:=data[i].id===0?'active':''%>"><%:=data[i].name%></a>
	<%}%>
</script>
<script id="search_init_template" type="text/html">
	<div id="search_box" class="clearfix">
		<%if(hot && hot.length>0){%>
		<div class="search_box_content hot">
					<span class="tit">
						热门搜索
					</span>
			<p class="key_word_list clearfix">
				<%for(var i in hot){%>
				<a href="javascript:;" tapmode onclick="searchWord('<%:=hot[i]%>',true)"><%:=hot[i]%></a>
				<%}%>
			</p>
		</div>
		<%}%>

		<%if(history && history.length>0){%>
		<div class="search_box_content history">
					<span class="tit">
						历史搜索
					</span>
					<span class="clear" tapmode onclick="clearWord()"></span>
			<ul class="key_word_list">
				<%for(var j in history){%>
				<li>
					<a href="javascript:;" tapmode onclick="searchWord('<%:=history[j]%>',true)"><%:=history[j]%></a>
					<span class="close" tapmode onclick="clearWord('<%:=history[j]%>')"></span>
				</li>
				<%}%>
			</ul>
		</div>
		<%}%>
	</div>
</script>
<script>
	var
		searching=false,
		pageData = {
			name: '搜索',
			history: [],
			hot: []
		},
		searchType = undefined,
		searchSetting = undefined,
		loadmore=function(obj){
			$('.loading_more').addClass('active');
			_$.ajax(ajaxSetting);
		},
		ajaxSetting = {
			interface: 'search',
			data: {
				tag:null,
				keyword:null,
				number:0,
				cnum:null
			},
			call: function (data) {
				$('.loading_more').removeClass('active');
				if(data.status===201){
					$('.loading_line').addClass('hide');
					if(!data.data && ajaxSetting.data.number===0){
						api.toast({
							msg: '没有找到"' + ajaxSetting.data.keyword + '"的相关内容'
						})
						init_search(pageData);
					}else if(ajaxSetting.data.number>0){
						api.toast({
							msg: '已经显示全部内容'
						})
					}
					$('.loading_line').addClass('hide');
					searching=false;
				}else{
					//检查一下关键字在历史里面是否存在，存在的话更新本地数据
					var fined = false;
					for (var i in pageData.history) {
						if (!fined && pageData.history[i] === ajaxSetting.data.keyword) {
							fined = true;
						}
					}
					if (!fined) {
						if (_$.getType(pageData.history) !== 'array') {
							pageData.history = [];
						}
						pageData.history.push(ajaxSetting.data.keyword);
						init_search(pageData);
					}

					var template = ajaxSetting.data.tag + '_template';

					_$.template(template,{data:data.data,img:_$.imgCache.default},function(html){
						var _room_=$('#search_main');
						if(getlistNum()>0){
							_room_.append(html);
						}else{
							_room_.html(html);
						}
						_$.imgCache.load();
					})

					searching = false;
					$('.loading_line').removeClass('hide');
					getlistNum();
				}
			}
		},
		clearWord=function(word){
			if(!word){
				word=pageData.history
			}else{
				word=[word]
			}
			_$.db.get({
				call:function(udb){
					if(udb.token===null){
						$('.search_box_content.history').remove();
					}else{
						_$.ajax({
							interface:'clearHistory',
							data:{
								type:searchType,
								keywords:word
							},
							call:function(data){
								if(data.status===200){
									_$.db.set({
										key:'search_keywords',
										val:data.data,
										call:function(){
											window.location.reload()
										}
									})
								}else{
									api.toast({
										msg:data.msg
									})
								}
							}
						})
					}
				}
			})
		},
		setSearchID=function(id,obj){
			if(id===0){
				ajaxSetting.data.cnum = null;
			}else{
				ajaxSetting.data.cnum = id;
			}
			searchWord($('#input').val());
		},
		searchWord = function (word,setKeyWord) {
			//没有关键字，初始化页面
			if(word===''){
				init_search(pageData);
				return false;
			}else if(searching){
				api.toast({
					msg: '搜索过于频繁，请慢一点。'
				})
				$('#input').val(ajaxSetting.data.keyword);
				return false;
			}else{
				//配置了设置关键字，那么是点击关键字的行为
				if(setKeyWord===true){
					$('#input').val(word);
				}
				//配置参数并发送请求
				ajaxSetting.data.keyword = word;
				ajaxSetting.data.number = getlistNum();
				searching=true;
				_$.ajax(ajaxSetting);
			}
		},
		init_nav = function () {
			/*
			_$.db.get({
				key: 'classify',
				call: function (data) {
					_$.template('classify_init_template', {data:data}, function (html) {
						$("#classroom_nav").html(html);
					})
				}
			})
			 */
		},
		getlistNum=function(){
			var num = $('.listnumitem').length;
			ajaxSetting.data.number=num;
			/*
			if(num===0){
				$('#classroom_nav a').removeClass('active').eq(0).addClass('active');
				ajaxSetting.data.cnum = null;
				$('.app_col_nav').hide();
			}else{
				$('.app_col_nav').show();
			}
			*/
		    return num;
		},
		init_search = function (data) {
			_$.template('search_init_template', data , function (html) {
				$('#search_main').html(html);
				$('.loading_line').addClass('hide');
			})
		},
		appready = function () {
			init_nav();

			searchSetting = api.pageParam;
			_$.db.get({
				key: 'area',
				call: function (data) {
					window.__areaData__ = data;
				}
			})
			_$.db.get({
				key: 'search_keywords',
				call: function (keywords) {
					searchSetting.data = keywords;
				}
			})
			if (searchSetting.win === 'topnews') {
				searchType = 'home_top_news';
				pageData.name = '艺考头条';
				pageData.history = searchSetting.data.top.history;
				pageData.hot = searchSetting.data.top.hot;
			} else if (searchSetting.frame === 'yuanxiao') {
				searchType = 'school_search';
				pageData.name = '院校';
				pageData.history = searchSetting.data.school_search.history;
				pageData.hot = searchSetting.data.school_search.hot;
			}else if (searchSetting.frame === 'classroom') {
				searchType = 'classroom_list';
				pageData.name = '课堂';
				pageData.history = searchSetting.data.lesson.history;
				pageData.hot = searchSetting.data.lesson.hot;
			} else if (searchSetting.frame === 'artcircle') {
				searchType = 'artcircle_list';
				pageData.name = '艺考圈消息';
				pageData.history = searchSetting.data.circle.history;
				pageData.hot = searchSetting.data.circle.hot;
			} else if (searchSetting.frame === 'mechanism') {
				searchType = 'mechanism_list';
				pageData.name = '机构';
				pageData.history = searchSetting.data.mechanism.history;
				pageData.hot = searchSetting.data.mechanism.hot;
			}
			ajaxSetting.data.tag = searchType;
			$('#input').attr('placeholder','搜索'+pageData.name);
			$('#input').bind('input propertychange', function() {
				searchWord($(this).val())
			});
			$('#close_search').on('click', function () {
				$('#input').val('');
				init_search(pageData);
				getlistNum();
			})
			init_search(pageData);
		};

</script>
<script src="../../lib/js/config.js"></script>
<script src="../../lib/js/jquery.js"></script>
<script src="../../lib/js/swiper.js"></script>
<script src="../../lib/js/common.js"></script>
</body>
</html>